<?php

use Drupal\shusy_taxation\Entity\ShusyTaxation;

function shusy_taxation_shusy_taxation_entity_form($form, &$form_state, ShusyTaxation $e, $op)
{
    $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_taxation') . '/shusy_taxation.forms.inc';
    $form_state[ShusyTaxation::class] = $e;
    $form['user'] = [
        '#type' => 'fieldset',
        '#title' => t('客户报单信息'),
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
    ];
    $form['user']['name'] = [
        '#type' => 'textfield',
        '#title' => t('姓名'),
        '#size' => 60,
        '#maxlength' => 128,
        '#required' => TRUE,
        '#default_value' => $e->name,
    ];
    $form['user']['store_name'] = [
        '#type' => 'textfield',
        '#title' => t('店名'),
        '#size' => 60,
        '#maxlength' => 32,
        '#required' => TRUE,
        '#default_value' => $e->store_name,
    ];

    $form['user']['sex'] = [
        '#type' => 'select',
        '#title' => t('性别'),
        '#options' => $e->getSexOptions(),
        '#default_value' => $e->sex,
        '#chosen' => TRUE,
    ];
    $form['user']['ages'] = [
        '#type' => 'textfield',
        '#title' => t('年龄'),
        '#size' => 60,
        '#maxlength' => 128,
        '#default_value' => $e->ages,
    ];
    $form['user']['identity'] = [
        '#type' => 'textfield',
        '#title' => t('身份证号'),
        '#size' => 60,
        '#maxlength' => 128,
        '#required' => TRUE,
        '#default_value' => $e->identity,
    ];
    $form['user']['phone'] = [
        '#type' => 'textfield',
        '#title' => t('联系电话'),
        '#size' => 60,
        '#maxlength' => 128,
        '#required' => TRUE,
        '#default_value' => $e->phone,
    ];
    $form['user']['store_title'] = [
        '#type' => 'textfield',
        '#title' => t('预做项目'),
        '#size' => 60,
        '#maxlength' => 128,
        '#required' => TRUE,
        '#default_value' => $e->store_title,
    ];
    $form['user']['doctor'] = [
        '#type' => 'textfield',
        '#title' => t('预约医生'),
        '#size' => 60,
        '#maxlength' => 128,
        '#required' => TRUE,
        '#default_value' => $e->doctor,
    ];
    $form['user']['remake'] = [
        '#type' => 'textfield',
        '#title' => t('消费情况'),
        '#size' => 60,
        '#maxlength' => 128,
        '#default_value' => $e->remake,
    ];

    $form['user']['faith'] = [
        '#type' => 'select',
        '#title' => t('信仰'),
        '#options' => [0=>'无',1=>'有'],
        '#default_value' => $e->faith,
        '#chosen' => TRUE,
    ];

    $form['user']['nowdate'] = [
        '#type' => 'date_popup',
        '#title' => t('预约时间'),
        '#default_value' => $e->nowdate,
        '#date_format' => 'Y-m-d',
        '#size' => 10,
        '#date_label_position' => 'none',
        '#date_increment' => 15,
        '#date_timezone'       => date_default_timezone(),
        '#date_year_range' => '2008:+1',
        '#description' => '',
        '#required' => false, 
    ];
    $form['userimgs'] = [
        '#type' => 'fieldset',
        '#title' => t('操作前图片'),
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
    ];
    $form['userimgs']['doctoroimg'] = [
        '#title' => t('正面图片'),
        '#type' => 'managed_file',
        '#default_value' => $e->doctoroimg,
        '#upload_location' => 'public://tmp/',
        '#required' => FALSE,
    ];
    $form['userimgs']['doctoroimg01'] = [
        '#type' => 'managed_file',
        '#default_value' => $e->doctoroimg01,
        '#upload_location' => 'public://tmp/',
        '#required' => FALSE,
    ];
    $form['userimgs']['doctoroimg02'] = [
        '#type' => 'managed_file',
        '#default_value' => $e->doctoroimg02,
        '#upload_location' => 'public://tmp/',
        '#required' => FALSE,
    ];


    $form['userimgs']['doctoroimgl'] = [
        '#title' => t('左侧面图片'),
        '#type' => 'managed_file',
        '#default_value' => $e->doctoroimgl,
        '#upload_location' => 'public://tmp/',
        '#required' => FALSE,
    ];
    $form['userimgs']['doctoroimgl01'] = [
        '#type' => 'managed_file',
        '#default_value' => $e->doctoroimgl01,
        '#upload_location' => 'public://tmp/',
        '#required' => FALSE,
    ];
    $form['userimgs']['doctoroimgl02'] = [
        '#type' => 'managed_file',
        '#default_value' => $e->doctoroimgl02,
        '#upload_location' => 'public://tmp/',
        '#required' => FALSE,
    ];



    $form['userimgs']['doctoroimgr'] = [
        '#title' => t('右侧面图片'),
        '#type' => 'managed_file',
        '#default_value' => $e->doctoroimgr,
        '#upload_location' => 'public://tmp/',
        '#required' => FALSE,
    ];
    $form['userimgs']['doctoroimgr01'] = [
        '#type' => 'managed_file',
        '#default_value' => $e->doctoroimgr01,
        '#upload_location' => 'public://tmp/',
        '#required' => FALSE,
    ];
    $form['userimgs']['doctoroimgr02'] = [
        '#type' => 'managed_file',
        '#default_value' => $e->doctoroimgr02,
        '#upload_location' => 'public://tmp/',
        '#required' => FALSE,
    ];





    $form['userimgh'] = [
        '#type' => 'fieldset',
        '#title' => t('操作后图片'),
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
    ];
    $form['userimgh']['doctoroimgs'] = [
        '#title' => t('正面图片'),
        '#type' => 'managed_file',
        '#default_value' => $e->doctoroimgs,
        '#upload_location' => 'public://tmp/',
        '#required' => FALSE,
    ];
    $form['userimgh']['doctoroimgs01'] = [
        '#type' => 'managed_file',
        '#default_value' => $e->doctoroimgs01,
        '#upload_location' => 'public://tmp/',
        '#required' => FALSE,
    ];
    $form['userimgh']['doctoroimgs02'] = [
        '#type' => 'managed_file',
        '#default_value' => $e->doctoroimgs02,
        '#upload_location' => 'public://tmp/',
        '#required' => FALSE,
    ];

    $form['userimgh']['doctoroimgsl'] = [
        '#title' => t('左侧面图片'),
        '#type' => 'managed_file',
        '#default_value' => $e->doctoroimgsl,
        '#upload_location' => 'public://tmp/',
        '#required' => FALSE,
    ];
    $form['userimgh']['doctoroimgsl01'] = [
        '#type' => 'managed_file',
        '#default_value' => $e->doctoroimgsl01,
        '#upload_location' => 'public://tmp/',
        '#required' => FALSE,
    ];
    $form['userimgh']['doctoroimgsl02'] = [
        '#type' => 'managed_file',
        '#default_value' => $e->doctoroimgsl02,
        '#upload_location' => 'public://tmp/',
        '#required' => FALSE,
    ];


    $form['userimgh']['doctoroimgsr'] = [
        '#title' => t('右侧面图片'),
        '#type' => 'managed_file',
        '#default_value' => $e->doctoroimgsr,
        '#upload_location' => 'public://tmp/',
        '#required' => FALSE,
    ];
    $form['userimgh']['doctoroimgsr01'] = [
        '#type' => 'managed_file',
        '#default_value' => $e->doctoroimgsr01,
        '#upload_location' => 'public://tmp/',
        '#required' => FALSE,
    ];
    $form['userimgh']['doctoroimgsr02'] = [
        '#type' => 'managed_file',
        '#default_value' => $e->doctoroimgsr02,
        '#upload_location' => 'public://tmp/',
        '#required' => FALSE,
    ];

    $form['status'] = [
        '#type' => 'select',
        '#title' => t('状态'),
        '#options' => $e->getStatusOptions(),
        '#default_value' => $e->status,
        '#chosen' => TRUE,
    ];
    $form['usertext'] = [
        '#type' => 'fieldset',
        '#title' => t('服务售后'),
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
    ];
    $form['usertext']['price'] = [
        '#type' => 'textfield',
        '#title' => t('消费积分'),
        '#size' => 60,
        '#maxlength' => 128,
        '#default_value' => $e->price,
    ];
    $form['usertext']['faiths'] = [
        '#type' => 'textarea',
        '#title' => t('服务项目明细'),
        '#size' => 60,
        '#maxlength' => 128,
        '#default_value' => $e->faiths,
    ];
    field_attach_form($e->entityType(), $e, $form, $form_state);

    $form['actions'] = ['#type' => 'actions'];
    $form['actions']['submit'] = [
        '#type' => 'submit',
        '#value' => t('保存'),
    ];
    return $form;
}


function shusy_taxation_shusy_taxation_entity_form_submit($form, &$form_state) {
    $e = $form_state[ShusyTaxation::class];
    if ($e instanceof ShusyTaxation) {
        entity_form_submit_build_entity($e->entityType(), $e, $form, $form_state);
        $e->save();
        $fids = [
            'doctoroimg',
            'doctoroimg01',
            'doctoroimg02',
            'doctoroimgl',
            'doctoroimgl01',
            'doctoroimgl02',
            'doctoroimgr',
            'doctoroimgr01',
            'doctoroimgr02',
            'doctoroimgs',
            'doctoroimgs01',
            'doctoroimgs02',
            'doctoroimgsl',
            'doctoroimgsl01',
            'doctoroimgsl02',
            'doctoroimgsr',
            'doctoroimgsr01',
            'doctoroimgsr02',
        ];
        foreach ($fids as $str) {
            $fid = $e->{$str};
            if (!empty($fid)) {
                $dir = "public://shusy_taxation/" . format_date(REQUEST_TIME, 'custom', 'Y/m/d/') . drupal_random_key();
                $result = file_prepare_directory($dir);
                if (empty($result)) {
                    drupal_mkdir($dir, 0777, TRUE);
                }
                $file = file_load($fid);
                if (!empty($file)) {
                    $file->status = 1;
                    file_save($file);
                    $ext = pathinfo($file->uri, PATHINFO_EXTENSION);
                    $file = file_move($file, "{$dir}/{$fid}.{$ext}");
                    file_usage_add($file, 'shusy_taxation', $str, $file->fid);
                }
            }
        }

    }
}
function shusy_taxation_shusy_taxation_delete_form($form, &$form_state, ShusyTaxation $e)
{
    $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_taxation') . '/shusy_taxation.forms.inc';
    $form_state[ShusyTaxation::class] = $e;
    return confirm_form($form,
        t('Are you sure you want to delete %title?', ['%title' => $e->label()]),
        'admin/content/shusy_taxation/' . $e->id,
        t('This action cannot be undone.'),
        t('Delete'),
        t('Cancel')
    );
}

function shusy_taxation_shusy_taxation_delete_form_submit($form, &$form_state)
{
    $e = $form_state[ShusyTaxation::class];
    if ($e instanceof ShusyTaxation) {
        $e->delete();
    }
    $form_state['redirect'] = 'admin/content/shusy_taxation';
}


function shusy_taxation_clear_form_submit($form, &$form_state)
{
    db_truncate('shusy_taxation')->execute();
    db_truncate('shusy_taxation_revision')->execute();
    $form_state['redirect'] = 'admin/content/shusy_taxation';
}
